package windows;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.Vector;

import base.Button;
import base.Window;

public class Blinds extends Window {
	
	private final static int SUNDAY = 0;
	private final static int MONDAY = 1;
	private final static int TUESDAY = 2;
	private final static int WEDNESDAY = 3;
	private final static int THURSDAY = 4;
	private final static int FRIDAY = 5;
	private final static int SATURDAY = 6;
	
	int current = SUNDAY;
	
	public int hour = 12, min = 30, ampm = 1;
	
	boolean set = false;
	boolean reset = false;
	boolean resetAll = false;

	private Vector<Button> buttons = new Vector<Button>( );
	public Blinds( )
	{
		super( "Blinds" , "Favorites" , "Bedroom[0]" , "Uncategorized" );
		
		// day
		Point dayUp = new Point(85, 5);
		Point dayDown = new Point(120, 5);
		
		//time
		buttons.add( new Button( "Hup" , new Point( 5 , 40 ) , "Images/bunny.png", false, true ) );
		buttons.add( new Button( "Mup" , new Point( 63 , 40 ) , "Images/bunny.png" , false, true ) );
		buttons.add( new Button( "Aup" , new Point( 115 , 40 ) , "Images/bunny.png" , false, true ) );
		buttons.add( new Button( "Hdown" , new Point( 5 , 115 ) , "Images/bunny.png", false, true ) );
		buttons.add( new Button( "Mdown" , new Point( 63 , 115 ) , "Images/bunny.png" , false, true ) );
		buttons.add( new Button( "Adown" , new Point( 115 , 115 ) , "Images/bunny.png", false, true ) );
		
		//open
		Point openUp = new Point(85, 150);
		Point openDown = new Point(120, 150);
		
		Point setLevel = new Point(5, 255);
		Point resetLevel = new Point(40, 255);
		Point resetAll = new Point(75, 255);
		
		Button dayUpArrow = new Button( "dayUpArrow", dayUp, "Images/bunny.png", false, true );
		Button dayDownArrow = new Button( "dayDownArrow", dayDown, "Images/bunny.png", false, true );
		//Button timeUpArrow = new Button( "timeUpArrow", timeUp, "Images/bunny.png", false, true );
		//Button timeDownArrow = new Button( "timeDownArrow", timeDown, "Images/bunny.png", false, true );
		Button openUpArrow =  new Button( "openUpArrow", openUp, "Images/bunny.png", false, true );
		Button openDownArrow = new Button( "openDownArrow", openDown, "Images/bunny.png", false, true );
		
		Button setButton = new Button( "Set", setLevel, "Images/bunny.png", true, true );
		Button resetButton = new Button( "Reset", resetLevel, "Images/bunny.png", true, true );
		Button resetAllButton = new Button( "Reset All", resetAll, "Images/bunny.png", true, true );
		
		setButton.setTextSize(10);
		resetButton.setTextSize(10);
		resetAllButton.setTextSize(10);
		
		buttons.add(dayUpArrow);
		buttons.add(dayDownArrow);
		//buttons.add(timeUpArrow);
		//buttons.add(timeDownArrow);
		buttons.add(openUpArrow);
		buttons.add(openDownArrow);
		
		buttons.add(setButton);
		buttons.add(resetButton);
		buttons.add(resetAllButton);		
	}
	
	@Override
	public void draw(Graphics2D g2d) {
		for( Button but : buttons )
			but.draw( g2d );
		// day
		//String day = current.name();
		String day = "";
		switch(current) {
		case SUNDAY:
			day = "Sunday";
			break;
		case MONDAY:
			day = "Monday";
			break;
		case TUESDAY:
			day = "Tuesday";
			break;
		case WEDNESDAY:
			day = "Wednesday";
			break;
		case THURSDAY:
			day = "Thursday";
			break;
		case FRIDAY:
			day = "Friday";
			break;
		case SATURDAY:
			day = "Saturday";
			break;
		}
		Font dayFont = new Font( "Console", Font.BOLD, 15);
		g2d.setFont(dayFont);
		g2d.drawString(day, 5, 25);
		
		// time
		Font big = new Font("NO", Font.PLAIN, 32);
		g2d.setFont(big);
		String output = "";
		if( hour < 10 )
			output += "0";
		output += hour;
		output += " : ";
		if( min < 10 )
			output += "0";
		output += min;
		if(ampm==1)
			output += " PM";
		else 
			output += " AM";
		g2d.drawString( output, 0, 105);
		
		// open
		
		int fontSize = 15;
		Font NEW = new Font( "Console", Font.BOLD, fontSize);
		g2d.setFont(NEW);
		String open = "open";
		g2d.drawString(open, 10, 150+fontSize);
	}

	@Override
	public void fingerPressed(Point location) {
		for( Button but : buttons ) {
			if( but.press( location.x , location.y ) ) {
				// day button handlers
				if( but.getName( ).equals( "dayUpArrow" ) ) {
					current = (current+1)%7;
				}
				if( but.getName( ).equals( "dayDownArrow" ) ) {
					current = (current-1)%7;
				}
				
				// open button handlers
				
				// set button
				
				// reset button
				
				// reset all button
				
				// time button handlers
				if( but.getName( ).equals( "Hup" ) ) {
					hour++;
					if(hour==13) {
						hour=1;
						//ampm=ampm*-1;
					}
					if(hour==12) {
						ampm=ampm*-1;
					}
				}
				if( but.getName( ).equals( "Mup" ) ) {
					min++;
					if(min==60) {
						++hour;
						min=0;
						if(hour==13) {
							hour=1;
						}
						if(hour==12) {
							ampm=ampm*-1;
						}
					}
				}
				if( but.getName( ).equals( "Aup" ) ) {
					ampm=ampm*-1;
				}
				if( but.getName( ).equals( "Hdown" ) ) {
					hour--;
					if(hour==0) {
						hour=12;
					}
					if(hour==12) {
						ampm=ampm*-1;
					}

				}
				if( but.getName( ).equals( "Mdown" ) ) {
					min--;
					if(min==0) {
						--hour;
						min=59;
						if(hour==0) {
							hour=12;
						}
						if(hour==12) {
							ampm=ampm*-1;
						}
					}
				}
				if( but.getName( ).equals( "Adown" ) ) {
					ampm=ampm*-1;
				}

				return;
			}
		}
	}

	@Override
	public void fingerReleased(Point location) {
		for( Button but : buttons )
			but.release( );
	}

	@Override
	public void gestureHorizontal(int amount) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void gestureVertical(int amount) {
		// TODO Auto-generated method stub
		
	}

}
